* Important: make sure to have the file "coord_mercator_europe.dta" and "idfile.dta" or however renamed in the same directory

*ssc install spmap
*ssc install shp2dta
*ssc install mif2dta
*ssc install Opencagegeo
*ssc install grmap 

clear all
use ESS.dta 
collapse Ingroup Monotonic ,by(country) 
replace country="Slovakia" if country=="Slovak Republic"
rename country country_name
save Mergy.dta, replace
merge 1:m country_name using "idfile.dta"
drop _merge

 drop if cname=="Morocco" |  cname=="Tunisia" |  cname=="Algeria"  |  cname=="Greenland"
 drop if ADMIN=="Greenland"

 
gen Sampling=0 if Ingroup!=.
replace  Sampling=0 if country_name=="Belarus" | country_name=="Lithuania" | country_name=="Latvia" | country_name=="Estonia"   | country_name=="Moldova"  | country_name=="Serbia"  | country_name=="Macedonia"  | country_name=="Albania"  | country_name=="Romania"  | country_name=="Bulgaria"  | country_name=="Georgia" | country_name=="Bosnia"   | country_name=="" | country_name=="" | country_name=="Bosnia and Herzegovina" | country_name=="Slovak rep" 

replace  Sampling=1 if country_name=="Italy" 

replace  Sampling=2 if country_name=="Finland" | country_name=="Belgium" | country_name=="Greece"  | country_name=="Ireland" | country_name=="Spain" | country_name=="Germany"| country_name=="Portugal"| country_name=="Sweden" |  country_name=="Switzerland" | country_name=="Iceland" 

replace  Sampling=3 if country_name=="Austria" | country_name=="Denmark"  | country_name=="France" | country_name=="Netherlands" | country_name=="United Kingdom" 


label variable Sampling "Effective sample"
la def immi2  0 "Excluded countries" 1 "Negative 1st stage" 2 "Weak 1st stage" 3 "Strong 1st stage", modify
la val Sampling immi2

  grstyle init
 grstyle set legend 2,  nobox
 grstyle set size 8pt: tick_label key_label
 grstyle set size 12pt: heading
 grstyle set size 10pt: subheading axis_title
 grstyle set graphsize 13cm 15cm

spmap Sampling using "coord_mercator_europe.dta", id(na_id_world) clmethod(unique)      clnumber(4)  fcolor(Greys2) osize(thin  thin  thin  thin) ndsize(thin) ndfcolor(white)     mfcolor(grs12)    legend(position(12) rows(2)  )  note("Country data comes for the ESS (1-9). Maps are drawn using official EU coordinates.", size(vsmall) span  )   plotregion(margin(5 0 0 7.5))  
graph export "Graph/Figure4_1a.tif", replace 

